//
/**
 * Filename: fig10-12_datatype_sigset_t.c
 * Author:   csbonkers
 * Contact:  forever65535@qq.com
 * Date:     2021/6/19
 * Description:
 *    信号集（sigset_t）数据类型相关操作函数
 *      模拟实现
 */
#include <stdio.h>
typedef int sigset_t;
#define NSIG 32
#include <errno.h>

#define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG)
#define sigemptyset(pset) (((*pset = 0)), 0)
#define sigfillset(pset) ((*pset = ~0), 0)
int sigaddset(sigset_t *pset, int signo) {
  if (SIGBAD(signo)) {
    errno = EINVAL;
    return -1;
  }
  *pset |= 1 << (signo - 1);
  return 0;
}
int sigdelset(sigset_t *pset, int signo) {
  if (SIGBAD(signo)) {
    errno = EINVAL;
    return -1;
  }
  *pset &= ~(1 << (signo - 1));
  return 0;
}
int sigismember(sigset_t *pset, int signo) {
  if (SIGBAD(signo)) {
    errno = EINVAL;
    return -1;
  }
  return (*pset & (1 << (signo - 1))) != 0;
}
int main(int argc, char *argv[]) {
  // test
  sigset_t sigset;
  int ret = sigemptyset(&sigset);
  printf("%u\n", ret);
  sigaddset(&sigset, 2);
  sigaddset(&sigset, 1);
  sigdelset(&sigset, 2);
  ret = sigemptyset(&sigset);
  printf("%u\n", sigset);
  return 0;
}